--
-- Author: shunguo.chen
-- Date: 2018-06-07 11:48:15
--
-- TheatreInfo 大剧院游戏数据

local TheatreTeam = import(".TheatreTeam")
local TheatreSession = import(".TheatreSession")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	self.myTeam = nil
	self.enemyTeam = nil
	self.teams = {TheatreTeam.new(), TheatreTeam.new()}
	self.sessions = {TheatreSession.new(), TheatreSession.new(), TheatreSession.new()}

	if data then
		self:init(data)
	end
end


-- //奇迹歌剧院状态
-- enum TheatreState {
-- 	TheatreInit 				= 0; //初始化
-- 	TheatreStart				= 1; //初始化
-- 	TheatrePVESession 			= 2; //PVE
-- 	TheatrePVPSession 			= 3; //PVP
-- 	TheatreEnd		 			= 4; //奇迹歌剧院结束
-- }

-- //Session状态
-- enum SessionState {
-- 	SessionInit 				= 0; //初始化
-- 	SessionRoleplayChoosing 	= 1; //选角色
-- 	SessionRoleplayChoosed 		= 2; //选角色完毕
-- 	SessionBattleStart 			= 3; //战斗开始
-- 	SessionBattleEnd 			= 4; //战斗结束
-- }

-- //奇迹歌剧院信息
-- message TheatreInfo {
-- 	int32	 sessionID      			= 1;//奇迹歌剧院场次
-- 	int32	 time              			= 2;//奇迹歌剧院剩余时间
-- 	TheatreState	 theatreState 		= 3;//奇迹歌剧场状态
--  repeated TheatreTeam teams 			= 4;//奇迹歌剧院双方队伍信息
--  repeated PVESessionInfo pveSessions = 5;//PVESession详情
--  SessionState sessionState		= 6;//PVPSession状态
-- }
function ClassRef:init(data)
	self.sessionID = data.sessionID
	self.time = data.time
	self.theatreState = data.theatreState
	self.sessionState = data.sessionState

	for i,v in ipairs(data.teams) do
		local team = self.teams[i]
		team:init(v)
		if team:isMyTeam() then
			self.myTeam = team
		else
			self.enemyTeam = team
		end
	end

	for i,v in ipairs(data.pveSessions) do
		local session = self.sessions[i]
		session:init(v)
		session:setMyTeamId(self.myTeam.teamId)
	end

	self._initTime = app:getTime()
end

-- 剩余时间
function ClassRef:getRestTime()
	local curTime = app:getTime()
	return self.time - math.ceil(curTime - self._initTime)
end

function ClassRef:getActionOverParams()
	-- 服务器记数从0开始
	return {sessionID=self.sessionID-1, sessionState=self.sessionState}
end

function ClassRef:getTeamName()
	return self.myTeam:getTeamName(), self.enemyTeam:getTeamName()
end

-- 当前是否是第一场
function ClassRef:isFirstSession()
	return self.sessionID == 1
end

-- 是开场loading
function ClassRef:isLoading()
	return self.theatreState == "TheatreStart"
end

-- 是否在npc说话中
function ClassRef:isNpcSpeak()
	return (self.theatreState == "TheatrePVESession" or self.theatreState == "TheatrePVPSession")
		and (self.sessionState == "SessionInit" or self.sessionState == "SessionStart")
end

-- 是否战斗中
function ClassRef:isInBattle()
	return (self.theatreState == "TheatrePVESession" or self.theatreState == "TheatrePVPSession")
		and (self.sessionState == "SessionRoleplayChoosed" or self.sessionState == "SessionBattleStart")
end

-- 当前是否是最后一场
function ClassRef:isLastSession()
	return self.sessionID == 4
end

function ClassRef:getSessions()
	return self.sessions
end

function ClassRef:getCurSession()
	return self.sessions[self.sessionID]
end

-- 更新我的队伍信息
function ClassRef:updateMyTeam(msg)
	self.myTeam:updateMembers(msg.members)
end

function ClassRef:getMyTeamLife()
	return self.myTeam.lifeNum
end
function ClassRef:setMyTeamLife(num)
	self.myTeam.lifeNum = num
end
function ClassRef:getEnemyTeamLife()
	return self.enemyTeam.lifeNum
end
function ClassRef:setEnemyTeamLife(num)
	self.enemyTeam.lifeNum = num
end

function ClassRef:getLifeData()
	local ret = {}
	ret.leftReviveTimes = self.myTeam.lifeNum
	ret.rightReviveTimes = self.enemyTeam.lifeNum
	return ret
end

function ClassRef:getTheateBg()
	if self.sessionID < 4 then
		local poolId = self:getTargetId()
		local conf = GD:queryTheatreTargetConf(poolId)
		return string.format(Res.theatreBgPath, conf.background)
	else
		return string.format(Res.theatreBgPath, "lugaofu_A")
	end
end

function ClassRef:getTargetId()
	if self.sessionID < 4 then
		return self.sessions[self.sessionID].theatrePoolID
	end
end

return ClassRef